/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-03-05 21:30:08
 * @filename : public/component/animation/module_animation.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module) {

    /*
       {
          type: 动画类型
          direction: 动画方向
          offset: 动画偏移量
          speed: 动画时间
          easing: 动画缓冲线
          display: true/false
          fn: callback
 
       }
    */

    var moduleAnimation = function() {
             
             this.opt = {
                  type : 'slide',
                  direction: '',
                  offset: $(window).width(),
                  speed: 300,
                  easing: 'linear',
                  display: true,
             };
 
             this.actionAnimation = function(currentM,nextM) {
                  var obj = this.getOpt(currentM,nextM);
                  //避免共享参数传达变量,异步问题.
                  if (obj.direction == 'left' && currentM) {
                      (function(obj,that){
                         that.left(obj);
                      })(obj,this);
                  } else if (obj.direction == 'right'&& currentM) {
                      (function(obj,that){
                         that.right(obj);
                      })(obj,this);
                  } else {
                      (function(obj,that){
                         that.defaults(obj);
                      })(obj,this);
                  }

             };

             this.getOpt = function(currentM,nextM) {
                  var opt = {};
                  if (nextM.ap) {
                      opt = {'direction':nextM.ap};
                  }
                  for (var i in opt) {
                      if (opt[i] !== undefined) {
                          this.opt[i] = opt[i];
                      }
                  }

                  var obj = this.opt;
                      obj.currentM = currentM;
                      obj.nextM = nextM;
 
                  return obj; 
             };

             this.defaults = function(obj) {
                  $('#'+obj.nextM.name).css({left:'0px','display':'block'});
             };

             this.left = function(obj) {
                
                if (obj.currentM) {
                    $('#'+obj.currentM.name).css({'left':'0px'});
                    $('#'+obj.currentM.name).animate({'left':-obj.offset+'px'},obj.speed,function(){
                        if (obj.display) {$(this).hide()};
                    });
                }

                $('#'+obj.nextM.name).css({'display':'block','left':obj.offset+'px'});
                $('#'+obj.nextM.name).animate({'left':'0px'},obj.speed);
             };

             this.right = function(obj) {

                if (obj.currentM) {
                    $('#'+obj.currentM.name).css({'left':'0px'});
                    $('#'+obj.currentM.name).animate({'left':obj.offset+'px'},obj.speed,function(){
                        if(obj.display) {$(this).hide()};
                    });
                }
                
                $('#'+obj.nextM.name).css({'display':'block','left':-obj.offset+'px'});
                $('#'+obj.nextM.name).animate({'left':'0px'},obj.speed);
             }; 

        };



    module.exports = new moduleAnimation;

});
